/*    
 * THis file is part of the xoperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.sf.net
 * Copyright (C) 2007  Jörg Unbehauen  

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package bitoflife.chatterbean.aiml;

import net.sf.xoperator.ui.aiml.AliceBotExternal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;

import bitoflife.chatterbean.Match;
/**
 * this class implements the External element of the aiml files. Gets calles whenever a <code><External></code> element in a template is encountered. When the process method is called, an ExternalListener that was previously registered in the AliceBotExternal with a matching name property is called to perform the specific task. 
 * @author joerg
 *
 */
public class External extends TemplateElement {

	private String name;

	private String param;

	private Log log = LogFactory.getLog(External.class);

	public External(Attributes attributes) {
		this.name = attributes.getValue("name");
		this.param = attributes.getValue("param");
	}

	public External(String name, String param) {
		this.name = name;
		this.param = param;
	}

	@Override
	public String process(Match match) {
		//Dirty hack, as the wildcard matching og the Graphmaster does not include the special characters, that form the last part of the query
		String response = ((AliceBotExternal) match.getCallback()).getListener(this.name)
				.perform(match, this.param);
		
		
		return response.substring(0,response.length()>1000?1000:response.length());

	}

}
